package com.suineg.pms.web.center;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.map.LinkedMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.suineg.pms.entity.base.Building;
import com.suineg.pms.entity.base.Community;
import com.suineg.pms.entity.base.Room;
import com.suineg.pms.service.base.BuildingService;
import com.suineg.pms.service.base.CommunityService;
import com.suineg.pms.service.base.RoomService;

/**
 * 首先引导
 *@author Suine
 */
@Controller
@RequestMapping(value = "/center")
public class BootController {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	private CommunityService communityService;
	
	private BuildingService buildingService;
	
	private RoomService roomService;
	
	@RequestMapping(value = "")
	@ResponseBody
	public String list(){
		List<Community> list = communityService.findAll();
		JSONArray array = new JSONArray();
		JSONArray buiArray = new JSONArray();
		JSONObject o = new JSONObject();
		try {
			JSONObject jobj = null;
			
			for(Community c : list){
				jobj = new JSONObject();
				jobj.put("id", c.getId());
				jobj.put("name", c.getName());
				array.put(jobj);
			}
			if(list != null && !list.isEmpty()){
				for(Building b : list.get(0).getBuildingList()){
					jobj = new JSONObject();
					jobj.put("id", b.getId());
					jobj.put("name", b.getName());
					
					buiArray.put(jobj);
				}
				
			}
			
			o.put("c", array);
			o.put("b", buiArray);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return o.toString();
	}
	
	@RequestMapping(value = "buildList/{id}")
	@ResponseBody
	public String getBuildList(@PathVariable("id") Long id,RedirectAttributes redirectAttributes){
		if(id == null || id == 0L){
			return "";
		}
		
		Community community = communityService.get(id);
		List<Building> buildList = community.getBuildingList();
		
		JSONArray buiArray = new JSONArray();
		try {
			JSONObject o = new JSONObject();
			for(Building b : buildList){
				o = new JSONObject();
				o.put("id", b.getId());
				o.put("name", b.getName());
				buiArray.put(o);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return "";
		}
		logger.info(buiArray.toString());
		return buiArray.toString();
	}
	
	/**
	 * 获取房间信息
	 * @return
	 */
	@SuppressWarnings({ "unused", "unchecked" })
	@RequestMapping(value = "detail/{id}")
	public String roomDetailByBuildingId(@PathVariable("id") Long id, Model model){
		Building building = buildingService.get(id);
		List<String> buildList = Lists.newArrayList();
		//保持顺序
		Map<String,List<Room>> map = new LinkedMap();
		//m<单元号,map>
		//map里有两个key cnt,对应一个Integer 是一个单元中一层的最大户数
		//              data,是具体的数据List<Room>
		Map<String,Map<String,Object>> m = new LinkedMap();
		//保持顺序
		Set<String> set = new LinkedHashSet<String>();
		List<Room> rl = null;
		for(Room r : building.getRoomList()){
			set.add(r.getUnit());
			rl = map.get(r.getUnit());
			if(rl == null || rl.isEmpty()){
				rl = Lists.newArrayList();
				rl.add(r);
				map.put(r.getUnit(), rl);
			}else{
				rl.add(r);
			}
			
			//新版
			Map<String,Object> mn = m.get(r.getUnit());
			if(mn == null || mn.isEmpty()){
				mn = new LinkedMap();
				mn.put("cnt", roomService.findRooMaxByBuidingId(id,r.getUnit()));
				rl = Lists.newArrayList();
				rl.add(r);
				mn.put("data", rl);
				m.put(r.getUnit(), mn);
			}else{
				List<Room> list = (List<Room>) mn.get("data");
				list.add(r);
			}
		}
		
		//新版 倒序排
		for(Map<String,Object> ms : m.values()){
			List<Room> lr = (List<Room>) ms.get("data");
			Collections.reverse(lr);
		}
		
		logger.info(m.toString());
		
		//倒序排
		for(List<Room> ll : map.values()){
			Collections.reverse(ll);
		}
		logger.info(map.toString());
		model.addAttribute("RESULT_MAP", m);
//		redirectAttributes.addAttribute("unitset", set);
		return "center/unitList";
	}
	
	public void handleUnit(List<Room> rooms){
		Set<String> unitSet = new HashSet<String>();
		for(Room r : rooms){
			unitSet.add(r.getUnit());
		}
		List<String> list = Lists.newArrayList();
		list.addAll(unitSet);
		Collections.sort(list);
	}

	@Autowired
	public void setCommunityService(CommunityService communityService) {
		this.communityService = communityService;
	}
	@Autowired
	public void setBuildingService(BuildingService buildingService) {
		this.buildingService = buildingService;
	}
	@Autowired
	public void setRoomService(RoomService roomService) {
		this.roomService = roomService;
	}
	
}

